
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import builder.AccountFormBuilder;
import builder.CoreAssetFormBuilder;
import builder.FormBuilder;
import builder.FormDirector;
import builder.RegisterDecorator;
import builder.RetrieveDecorator;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.awt.Font;


public class LoginUI extends JPanel {
	public static JTextField GlobalUsername;
	private JTextField txtPassword;
	int checker;
	
	private AssetRegistryFrame arFrame;
    DBConnector db = new DBConnector();
	
	/**
	 * Create the panel.
	 */
	public LoginUI(AssetRegistryFrame arFrame ) {
		this.arFrame = arFrame;
		
		setLayout(null);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblUsername.setBounds(348, 213, 78, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPassword.setBounds(348, 301, 78, 14);
		add(lblPassword);
		
		GlobalUsername = new JTextField();
		GlobalUsername.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
					btnSubmitActionPerformed();
			}
		});
		GlobalUsername.setBounds(287, 238, 201, 30);
		add(GlobalUsername);
		GlobalUsername.setColumns(10);
		
		txtPassword = new JPasswordField();
		txtPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
					btnSubmitActionPerformed();
			}
		});
		txtPassword.setBounds(287, 326, 201, 30);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnSubmitActionPerformed();
			}
		});
		btnSubmit.setBounds(287, 400, 89, 23);
		add(btnSubmit);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnRegisterActionPerformed(arg0);
			}
		});
		btnRegister.setBounds(399, 400, 89, 23);
		add(btnRegister);
		
		JLabel lblLogin = new JLabel("LOGIN");
		lblLogin.setFont(new Font("Impact", Font.BOLD, 70));
		lblLogin.setBounds(303, 34, 234, 78);
		add(lblLogin);

	}
	
	private void btnSubmitActionPerformed() {
		String query = "SELECT COUNT(*) AS user FROM useraccounts WHERE username = '"+GlobalUsername.getText()+"' and password = '"+txtPassword.getText()+"'";
		Connection con = DBConnector.getConnection();
		ResultSet rs = DBConnector.executeQuery(con, query);
		
		try {
			while(rs.next())
			    checker = rs.getInt("user");
			
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		if(checker == 1){
			GlobalUsername.setText(GlobalUsername.getText().trim());
			arFrame.changePanel(new MenuUI(arFrame));
		}
		else if(GlobalUsername.getText().isEmpty() || txtPassword.getText().isEmpty())
			DBConnector.alertUser("Please complete the text fields.");
		else
			DBConnector.alertUser("Incorrect Username or Password.");
	}
	
	private void btnRegisterActionPerformed(ActionEvent arg0) {
		//arFrame.changePanel(new RegistrationUI(arFrame));
		
		FormBuilder builder = new AccountFormBuilder();
		builder = new RegisterDecorator(builder);
    	FormDirector director = new FormDirector(builder);
    	director.construct();
		JPanel panel = director.getForm();
		
		arFrame.changePanel(panel);
	}
}
